package com.example.recruit.fiter;


import cn.hutool.core.text.AntPathMatcher;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.example.recruit.common.GlobalExceptionHandler;
import com.example.recruit.common.JurisdictionException;
import com.example.recruit.common.R;
import com.example.recruit.util.BaseThreadLocal;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@Slf4j
//@WebFilter(filterName = "checkLoginFilter",urlPatterns = "/*")
//启动类记得加注解 @ServletComponentScan
public class CheckLoginFilter implements Filter {
    //路径匹配器
    public  static final AntPathMatcher PATH_MATCHER=new AntPathMatcher();

//    * HR类
// * login 登录
// * getCode 获取验证码
// * register 注册
// * searchAll 查询雇主所属公司数据
// * getUserById 通过id查询用户
/**
 * @author WEI
 * 工作类
 * getListJob   获取工作列表
 * updateStatus 修改工作状态
 * deleteJob 根据id删除工作
 * addJob 添加工作
 * employeeAddJobList 查询hr发布的工作
 */
    /**
     * 用户管理类
     * loginUser
     * getCode
     * registerUser
     */
    String [] paths=new String[]{
            "/employer/login",
            "/user/login",
            "/user/register",
            "/employer/getCode",
            "/employer/register",
            "/user/getCode",
            "/job/getListJob",
            "/employer/searchAll",
            "/doc.html",
            "/webjars/**",
            "/swagger-resources",
            "/v2/api-docs"

    };
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String requestURI=request.getRequestURI();
         requestURI = requestURI.replaceAll("//","/");
        boolean flag = check(paths, requestURI);
        log.info("拦截的请求"+requestURI+"=="+flag);
        HttpSession session = request.getSession();

        String isEmployer = (String) session.getAttribute("employee");
        if(flag){
            log.info("不需要处理的请求"+requestURI);
            filterChain.doFilter(request,response);
        }else if(isEmployer!=null){
            log.info("管理员");
            filterChain.doFilter(request,response);
        }
        else {
            log.info("用户");
            response.setContentType("text/json;charset=UTF-8");
            response.getWriter().write(JSON.toJSONString(R.error("权限异常")));
            return;
        }

    }

    /**
     * 路径匹配，检查本次请求是否需要执行
     * @param urls
     * @param requestURI
     * @return
     */
    public boolean check(String [] urls,String requestURI){
        for (String url : urls) {
            boolean match = PATH_MATCHER.match(url, requestURI);
            if(match){
                return  true;
            }
        }
        return  false;
    }
}
